package com.flyme.module.base.provider.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.common.core.utils.ObjectUtils;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.base.client.api.SysContinuecourseRemoteService;
import com.flyme.module.base.client.entity.SysContinuecourse;
import com.flyme.module.base.client.entity.SysStudyconfig;
import com.flyme.module.base.provider.service.SysContinuecourseService;
import com.flyme.module.base.provider.service.SysStudyconfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 继续教育课程中间管理
 * @author zyh
 * @date 2020-9-4
 */
@Api(tags = "继续教育课程中间")
@RestController
public class SysContinuecourseController extends BaseController<SysContinuecourseService, SysContinuecourse> implements SysContinuecourseRemoteService {

    @Autowired
    private SysStudyconfigService studyconfigService;

    /**
     * 继续教育课程列表
     */
    @ApiOperation(value = "获取课程分页列表", notes = "获取课程分页列表")
    @Override
    public ResultBody list(@ModelAttribute(value = "pagerInfo") PagerInfo pagerInfo, SysContinuecourse course) {
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("cc.*, course.courseTitle");
        cq.createAlias("course", "course");
        cq.eq("certificateId", course.getCertificateId());
        PqGrid pqGrid = bizService.pageList(cq);
        return ResultBody.success(pqGrid);
    }

    /**
     * 继续教育课程列表
     */
    @Override
    public ResultBody delById(@PathVariable Long id) {
        Boolean tag = bizService.removeById(id);
        if(tag){
            return ResultBody.success("操作成功");
        }else{
            return ResultBody.failed("操作失败");
        }
    }

    /**
     * 添加/更新继续教育课程
     */
    @Override
    public ResultBody addContinueCourse(SysContinuecourse course) {
        if(ObjectUtils.isEmpty(course.getId())){
            Integer count = bizService.count(q().eq("certificateId", course.getCertificateId()).eq("courseId", course.getCourseId()));
            if(count > 0){
                return ResultBody.failed("该课程已添加");
            }
        }
        Boolean tag = bizService.saveOrUpdate(course);
        return ResultBody.success("添加成功").setData(tag);
    }

    /**
     * 重置继续教育课程
     */
    @Override
    public ResultBody resetContinueCourse(@PathVariable Long certificateId) {
        List<SysContinuecourse> list = bizService.list(q().eq("certificateId", certificateId));
        String courseIds = "";
        if(list.size() > 0){
            for(SysContinuecourse item : list){
                courseIds =  item.getCourseId() + "," + courseIds;
            }
            courseIds = courseIds.substring(0, courseIds.length() - 1);
        }
        studyconfigService.update(new UpdateWrapper<SysStudyconfig>().set("courseIds",courseIds).eq("syscertificateId",certificateId));
        return ResultBody.success("操作成功");
    }
}
